package edu.ccut.saturn.studio.naviagtor.properties;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;

import edu.ccut.saturn.studio.naviagtor.SaturnDirectory;

/**
 * 定义属性页。 参见org.eclipse.ui.propertyPages 扩展点。
 * 
 * @author alf 2008-9-1
 */
public class SaturnModelPropertyPage extends PropertyPage implements
		IWorkbenchPropertyPage {

	private Text nameText;
	private Text versionText;
	private Text descriptionText;
	private Text typeText;
	private Text timeText;
	private SaturnDirectory saturnDirectory;

	public SaturnModelPropertyPage() {
	}

	@Override
	protected Control createContents(Composite parent) {
		noDefaultAndApplyButton();

		IResource resource = ((org.eclipse.jdt.internal.core.PackageFragment) getElement())
				.getResource();

		if (resource instanceof IFolder) {
			IFile file = ((IFolder) resource).getFile(".saturn");
			if (file.exists()) {
				saturnDirectory = SaturnDirectory.loadSaturnDirectory(file);

				return addControl(parent, saturnDirectory);
			}
		}

		return null;
	}

	private Control addControl(Composite parent, SaturnDirectory saturnDirectory) {
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayout(new GridLayout());
		Group group = new Group(composite, SWT.NONE);
		final GridData gd_group = new GridData(SWT.FILL, SWT.FILL, false, false);
		gd_group.widthHint = 495;
		group.setLayoutData(gd_group);
		group.setText("Saturn Folder Information");
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		group.setLayout(gridLayout);
		Label label = new Label(group, SWT.RIGHT);
		label.setText("Version");
		versionText = new Text(group, SWT.SINGLE);
		GridData gridData = new GridData(SWT.FILL, SWT.CENTER, false, false);
		gridData.widthHint = 150;
		versionText.setLayoutData(gridData);
		versionText.setText(saturnDirectory.getVersion());

		label = new Label(group, SWT.RIGHT);
		label.setText("Name");
		nameText = new Text(group, SWT.LEFT);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		nameText.setLayoutData(gridData);
		nameText.setText(saturnDirectory.getName());

		label = new Label(group, SWT.RIGHT);
		label.setText("Description");
		descriptionText = new Text(group, SWT.LEFT);
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		descriptionText.setLayoutData(gridData);
		descriptionText.setText(saturnDirectory.getDescription());

		label = new Label(group, SWT.RIGHT);
		label.setText("Type");
		typeText = new Text(group, SWT.LEFT);
		gridData = new GridData();
		typeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL));
		typeText.setText(saturnDirectory.getType());

		label = new Label(group, SWT.RIGHT);
		label.setText("Time");
		timeText = new Text(group, SWT.LEFT);
		gridData = new GridData();
		timeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL));
		timeText.setText(saturnDirectory.getTime());
		timeText.setEditable(false);
		return composite;

	}

	@Override
	public boolean performOk() {
		return super.performOk();
	}
}
